using System;
using System.IO;
using System.Text;

namespace LuckyBingoRcon.Utilities
{
	/// <summary>
	/// Singleton debug logger.
	/// </summary>
	public class Logger
	{
		public static readonly Logger Log = new Logger();

		private int count = 0;
		private string timeFormat = "yyyy-MM-dd-HH-mm-ss";
		private TextWriter tw = null;

		private Logger()
		{
			string filename = DateTime.UtcNow.ToString(timeFormat) + ".log";
			tw = TextWriter.Synchronized(new StreamWriter(new FileStream(filename, FileMode.OpenOrCreate)));
		}

		public void WriteLine(string message)
		{
			try
			{
				string line = this.FormatMessage(message);
				tw.WriteLine(line);
				tw.Flush();
			}
			catch
			{
				// eat it
			}
		}

		public void Write(string message)
		{
			try
			{
				string line = this.FormatMessage(message);
				tw.Write(line);
				tw.Flush();
			}
			catch
			{
				// eat it
			}
		}

		public void WriteBlankLine()
		{
			try
			{
				tw.WriteLine();
				tw.Flush();
			}
			catch
			{
				// eat it
			}
		}

		private string FormatMessage(string message)
		{
			string line = DateTime.UtcNow.ToString(timeFormat);
			line += "\t" + count + "\t";
			line += message;
			return line;
		}
	}
}
